package org.itboys.baseobject.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.itboys.baseobject.entity.City;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

/**
 * 省市县镇街道业务层
 * @author weisky
 *
 */
@Service
public class CityService extends BaseService<City, Long>{

	private static final long serialVersionUID = 2259128860302609447L;

	@Resource(name = "baseMongoDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<City> getEntityClass() {
		return City.class;
	}
	
	/**
	 * 获取所有的省份
	 * @return
	 */
	public List<City> findAllProvince(){
		try{
			return super.findByField("levelType", 1);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("CityService.findAllProvince error", e);
		}
		return null;
	}
	
	/**
	 * 获取省份下的所有市区
	 * @param provinceId
	 * @return
	 */
	public List<City> findAllCity(long provinceId){
		try{
			Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
			param.put("levelType", 2);
			param.put("parentId", provinceId);
			return super.list(param);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("CityService.findAllCity error", e);
		}
		return null;
	}
	
	/**
	 * 获取所有市下的县/区
	 * @param cityId
	 * @return
	 */
	public List<City> findAllcounty(long cityId){
		try{
			Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
			param.put("levelType", 3);
			param.put("parentId", cityId);
			return super.list(param);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("CityService.findAllcounty error", e);
		}
		return null;
	}
	
	/**
	 * 获取县/区下的所有镇/街道
	 * @param countyId
	 * @return
	 */
	public List<City> findAllTown(long countyId){
		try{
			Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
			param.put("levelType", 4);
			param.put("parentId", countyId);
			return super.list(param);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("CityService.findAllTown error", e);
		}
		return null;
	} 
	
	/**
	 * 获取城市信息的通用方法
	 * @param param
	 * @return
	 */
	public List<City> findCityGeneral(Map<String, Object> param){
		try{
			return super.list(param);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("CityService.findCityGeneral error", e);
		}
		return null;
	}  

}
